package com.tianchi.lexiang.util;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class SyncTableUtil {
	private static final String TAG = "SyncTableUtil";
	private static SyncTableUtil instence=new SyncTableUtil();
	private SQLiteDatabase db=DBOperator.getInstance().getConnection();
	
	public SyncTableUtil(){
		
	}
	
	public static SyncTableUtil getInstence(){
		return instence;
	}
	
	/**
	 * already point table synchronous
	 * @param cmdContent
	 */
	public void syncAlreadyPointTable(String cmdContent){
		Log.d(TAG, "[syncAlreadyPointTable] cmdContent=>"+cmdContent);
		String [] alreadPoint;
		String [] alreadyPointInfo;
		ContentValues values=new ContentValues();
		if(db.delete("T_BDSM_ALREADY_POINT", null, null) >= 0){
			if(cmdContent!=null&&!"".equals(cmdContent)){
				alreadPoint=cmdContent.split("\\|");
				Log.d(TAG, "alreadPoint.length==>"+alreadPoint.length);
				for(int i=0;i<alreadPoint.length;i++){
					alreadyPointInfo=alreadPoint[i].split(",");
					if (alreadyPointInfo.length == 3) {
						values.put("SONG_ID", alreadyPointInfo[0]);
						values.put("SONG_NAME", alreadyPointInfo[1]);
						values.put("ALREADY_POINT_ORDER", alreadyPointInfo[2]);
						long res = db.insert("T_BDSM_ALREADY_POINT", null, values);
						Log.d(TAG, "insert "+res);
					}
				}
			}
		}
	}
	
	/**
	 * public song table synchronous
	 * @param cmdContent
	 */
	public void syncPublicSongTable(String cmdContent){
		Log.d(TAG, "[syncPublicSongTable] cmdContent ="+cmdContent);
		String [] publicSong;
		String [] publicSongInfo;
		ContentValues values = new ContentValues();
		if(db.delete("T_BDSM_PUBLIC_SONG", null, null) >= 0){
			if(cmdContent!=null&&!"".equals(cmdContent)){
	           publicSong = cmdContent.split("\\|");
	           Log.d(TAG, "publicSong.length=>"+publicSong.length);
	           for(int i=0;i<publicSong.length;i++){
	        	   publicSongInfo=publicSong[i].split(",");
	        	   if (publicSongInfo.length == 2) {
	        		   values.put("SONG_ID", publicSongInfo[0]);
	        		   values.put("SONG_NAME", publicSongInfo[1]);
	        		   Log.d(TAG, "[SyncPublicSongTable] will insert ");
	        		   long rowid = db.insert("T_BDSM_PUBLIC_SONG", null, values);
	        		   Log.d(TAG, "[syncPublicSongTable]rowid=>"+rowid);
	        	   }
	           }
			}
		}
	}
	
	/**
	 * osdtitle table synchronous
	 * @param cmdContent
	 */
	public void syncOsdSubtitleTable(String cmdContent){
		Log.d(TAG, "[syncOsdSubtitleTable] cmdContent ="+cmdContent);
		String [] osdsUBtitleInfo;
		
		if(db.delete("T_BDSM_OSD_SUBTITLES", null, null) >= 0){
			if(cmdContent!=null&&!"".equals(cmdContent)){
				osdsUBtitleInfo=cmdContent.split("\\|");
				for(int i=0;i<osdsUBtitleInfo.length;i++){
					ContentValues values=new ContentValues();
					values.put("OSD_SUBTITLES_CONTENT", osdsUBtitleInfo[i]);
					db.insert("T_BDSM_OSD_SUBTITLES", null, values);
				}
			}
		}
	}

}
